//
//  ViewController.m
//  CollectionView可移动item
//
//  Created by dllo on 16/8/11.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "CustomCollectionViewCell.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    _dataArray = [NSMutableArray array];
    for (int i = 0; i < 100; i++) {
        [_dataArray addObject:[NSString stringWithFormat:@"%d", i]];
    }
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.collectionView addGestureRecognizer:longPress];
}
#pragma mark - 长按的手势执行的方法
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:{
            // 获取按住的点
            CGPoint point = [longPress locationInView:_collectionView];
            // 根据点找到对应的indexPath
            NSIndexPath *indexPath = [_collectionView indexPathForItemAtPoint:point];
            // 假如没有这个item, 返回
            if (indexPath == nil) {
                break;
            }
            // 我的CollectionView交换item的位置
            [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
        }
            break;
            // 更新位置
            case UIGestureRecognizerStateChanged:
            [self.collectionView updateInteractiveMovementTargetPosition:[longPress locationInView:_collectionView]];
            break;
            // 结束移动
            case UIGestureRecognizerStateEnded:
            [self.collectionView endInteractiveMovement];
            break;
        default:
            // 什么都没做的时候取消 item 的交换
            [self.collectionView cancelInteractiveMovement];
            break;
    }
}
#pragma mark - 可编辑的状态
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
#pragma mark - 移动的方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    // 获取要操作的字符串
    NSString *sourceStr = _dataArray[sourceIndexPath.row];
    [_dataArray removeObjectAtIndex:sourceIndexPath.row];
    [_dataArray insertObject:sourceStr atIndex:destinationIndexPath.row];
}
#pragma mark - 选中item时执行的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [_dataArray removeObjectAtIndex:indexPath.row ];
    [_collectionView performBatchUpdates:^{
        [_collectionView deleteItemsAtIndexPaths:@[indexPath]];
    } completion:nil];
}



#pragma mark - datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.label.text = _dataArray[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
